import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import MsgBoxIcon 1.0
import QtWebEngine 1.4
import ConfigElementType 1.0


WebEngineView {

    Timer {
        interval: 500; running: true; repeat: true
        onTriggered: {
            if (aaFlowcharWebview.loadProgress === 100) {
                var command = "document.getElementById('flowchart_running_cmd').value";
                if (aaFlowcharWebview.title === "Flow chart") { //Avoid any error message flushing
                    aaFlowcharWebview.runJavaScript(command, function(result) {
                        command = "document.getElementById('flowchart_running_cmd').value = ''";
                        if (result.length > 0) {
                            var obj = JSON.parse(result)
                            var operatorId = obj["operatorId"]
                            tem.runSingleCmd(aaCore, "performManualTest", [operatorId, result])
                            aaFlowcharWebview.runJavaScript(command, function(result) {
                                //This is just reset the command in flowchart js
                            })
                        }
                    })
                }
            }
        }
    }

    property bool isFirstLoad: true
    anchors.centerIn: parent
    width: parent.width
    height: parent.height
    url: "file:///C:/flowchart/flowchart.html"
    onLoadProgressChanged: {
        if (loadProgress === 0) {
            console.log("Flowchart start to laod")
        } else if (loadProgress === 100 && isFirstLoad) {
            isFirstLoad = false
            console.log("Flowchart load complete")
            if (aaCoreConfig.currentFlowchartFilename.length > 0) {
                console.log("Trying to load : " + aaCoreConfig.currentFlowchartFilename)
                file.setSource(aaCoreConfig.currentFlowchartFilename)
                var result = file.read()
                var command = "document.getElementById('flowchart_data').value ='" + result + "'";
                aaFlowcharWebview.runJavaScript(command, function(result) {
                    console.log("Load flowchart success")
                    command = "document.getElementById('set_data').click()"
                    aaFlowcharWebview.runJavaScript(command)
                    aaCore.loadFlowchart(result)
                })
            }
        }
    }
}
